package com.zxc.controller.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zxc.Util.windWeather;
import com.zxc.model.*;
import com.zxc.model.Crops.CropsVO;
import com.zxc.model.ParkDaily.ParkState;
import com.zxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RestController
@RequestMapping("/user")
public class userController {
//    上面表示进入这个容器的映射路径是 /user


//    下面表示自动引入这个UserService 的实现接口 好比new了一个UserService的对象出来
    @Autowired
    private UserService userService;

//下面是进入这个getPark 的方法映射路径 使用@RequestParam 因为我们只需要传一个值就可以了。所以不用 @RequestBody，并且这里前端使用的是get
//    假如你前端是post过来的数据 并且数据比较多，比如你是要修改数据或者更改数据，那用@RequestBody会好一些，这个参考login的Controller
    @RequestMapping("/getPark")
    public ResponseEntity<?> getPark(@RequestParam(value = "account") String account) {
//        获取到的是账号，通过账号在解析成他的id
//        String userid=userService.getUser(account).getId();
//        在通过id去查询他的园区。
//        List<ParkDetails> ParkDetailsList = userService.getParkByUserId(userid);
//         返回成功页面。我们先用他自带的。后面我们在自定义这个ResponseEntity
//        return ResponseEntity.ok(ParkDetailsList);
        return ResponseEntity.ok(userService.getParkByUserId(userService.getUser(account).getId()));
    }
//
    @RequestMapping("/updatePark")
    public ResponseEntity<?> updatePark(@RequestBody ParkDetails parkDetails) {
        return ResponseEntity.ok(userService.updatePark(parkDetails));
    }

    @RequestMapping("/getParkByUser")
    public ResponseEntity<?> getParkByUser(@RequestBody ParkDetails parkDetails) {
        return ResponseEntity.ok(userService.getParkByUser(parkDetails));
    }

    @RequestMapping("/getParkOne")
    public ResponseEntity<?> getParkOne(@RequestBody ParkState parkState) {
        return ResponseEntity.ok(userService.getParkOne(parkState));
    }

    @RequestMapping("/getUserList")
    public ResponseEntity<?> getUserList(@RequestBody User user) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =user.getPageList().get(0);
        PageHelper.startPage(page.getOffset(), page.getLimit());
        response.put("UserList",new PageInfo<User>(userService.getUserList(user)));
        return ResponseEntity.ok(response);
    }

    @RequestMapping("/getUserListByUser")
    public ResponseEntity<?> getUserListByUser(@RequestBody User user) {
        return ResponseEntity.ok(userService.getUserList(user));
    }
//仓库申请的下拉值开始
    @RequestMapping("/getSeed")
    public ResponseEntity<?> getSeed() {
    seeds s =new seeds();
    return ResponseEntity.ok(userService.getSeedList(s));
}

    @RequestMapping("/getTools")
    public ResponseEntity<?> getTools() {
    Tools tools =new Tools();
        return ResponseEntity.ok(userService.getToolsList(tools));
    }

    @RequestMapping("/getFertilizer")
    public ResponseEntity<?> getFertilizer() {
        Fertilizer f =new Fertilizer();
        return ResponseEntity.ok(userService.getFertilizerList(f));
    }

    @RequestMapping("/getPesticide")
    public ResponseEntity<?> getPesticide() {
                Pesticide p =new Pesticide();
        return ResponseEntity.ok(userService.getPesticideList(p));
    }
    //仓库申请的下拉值结束

    //通过种子对象获取种子仓库信息
    @RequestMapping("/getSeedWarehouse")
    public ResponseEntity<?> getseedswarehouse(@RequestBody seeds s) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =s.getPageList().get(0);
//        添加数据
        if("root".equals(page.getAccount())){
            PageHelper.startPage(page.getOffset(), page.getLimit());
            response.put("SeedList",new PageInfo<seeds>(userService.getSeedList(s)));
        }else{
            PageHelper.startPage(page.getOffset(), page.getLimit());
            s.setUserId(userService.getUser(page.getAccount()).getId());
            response.put("SeedList",new PageInfo<seeds>(userService.getUserSeedsList(s)));
        }
        return ResponseEntity.ok(response);
    }
    //通过农具对象获取农具仓库信息
    @RequestMapping("/getToolsWarehouse")
    public ResponseEntity<?> gettoolswarehouse(@RequestBody Tools t) {

        Map<String, Object> response = new HashMap<String, Object>();

        Page page =t.getPageList().get(0);


//        添加数据
        if("root".equals(page.getAccount())){
            PageHelper.startPage(page.getOffset(), page.getLimit());
            response.put("ToolsList",new PageInfo<Tools>(userService.getToolsList(t)));
        }else{
            PageHelper.startPage(page.getOffset(), page.getLimit());
            t.setUserId(userService.getUser(page.getAccount()).getId());
            response.put("ToolsList",new PageInfo<Tools>(userService.getUserToolsList(t)));
        }
        return ResponseEntity.ok(response);
    }
    //通过肥料对象获取肥料仓库信息
    @RequestMapping("/getfertilizerWarehouse")
    public ResponseEntity<?> getfertilizerWarehouse(@RequestBody Fertilizer f) {

        Map<String, Object> response = new HashMap<String, Object>();

        Page page =f.getPageList().get(0);
//        添加数据
        if("root".equals(page.getAccount())){
            PageHelper.startPage(page.getOffset(), page.getLimit());
            response.put("FertilizerList",new PageInfo<Fertilizer>(userService.getFertilizerList(f)));
        }else{
            f.setUserId(userService.getUser(page.getAccount()).getId());
            PageHelper.startPage(page.getOffset(), page.getLimit());
            response.put("FertilizerList",new PageInfo<Fertilizer>(userService.getUserFertilizerList(f)));
        }
        return ResponseEntity.ok(response);
    }
    //通过农药对象获取农药仓库信息
    @RequestMapping("/getpesticidelistWarehouse")
    public ResponseEntity<?> getpesticideWarehouse(@RequestBody Pesticide p) {

        Map<String, Object> response = new HashMap<String, Object>();

        Page page =p.getPageList().get(0);
//        添加数据
        if("root".equals(page.getAccount())){
            PageHelper.startPage(page.getOffset(), page.getLimit());
            response.put("PesticideList",new PageInfo<Pesticide>(userService.getPesticideList(p)));
        }else{
            PageHelper.startPage(page.getOffset(), page.getLimit());
            p.setUserId(userService.getUser(page.getAccount()).getId());
            response.put("PesticideList",new PageInfo<Pesticide>(userService.getUserPesticideList(p)));
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/insertPesticideDate")
    public ResponseEntity<?> insertPesticideDate(@RequestBody Pesticide p) {
        Map<String, Object> response = new HashMap<String, Object>();
        userService.setPesticide(p);
        return ResponseEntity.ok(response);
    }

    //管理员插入仓库数据
    @RequestMapping("/insertPesticideWarehouseDate")
    public ResponseEntity<?> insertPesticideWarehouseDate(@RequestBody Pesticide p) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =p.getPageList().get(0);
        if("root".equals(page.getAccount())){
            userService.setPesticideWarehouse(p);
        }else{
            userService.setUserPesticideWarehouse(p);
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/insertFertilizerWarehouseDate")
    public ResponseEntity<?> insertFertilizerWarehouseDate(@RequestBody Fertilizer f) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =f.getPageList().get(0);
        if("root".equals(page.getAccount())){
            userService.setFertilizerWarehouse(f);
        }else{
            userService.setUserFertilizerWarehouse(f);
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/insertSeedsWarehouseDate")
    public ResponseEntity<?> insertSeedsWarehouseDate(@RequestBody seeds s) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =s.getPageList().get(0);
        if("root".equals(page.getAccount())){
            userService.setSeedsWarehouse(s);
        }else{
            userService.setUserSeedsWarehouse(s);
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/insertToolsWarehouseDate")
    public ResponseEntity<?> insertToolsWarehouseDate(@RequestBody Tools t) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =t.getPageList().get(0);
        if("root".equals(page.getAccount())){
            userService.setToolsWarehouse(t);
        }else{
            userService.setUserToolsWarehouse(t);
        }
        return ResponseEntity.ok(response);
    }
    //插入园区日常信息记录及园区治疗物使用记录
    @RequestMapping("/insertParkDailyInfo")
    public ResponseEntity<?> recordprakdailyinfo(@RequestBody ParkState ps) {
        Page page =ps.getPageList().get(0);
        ps.setUserId(userService.getUser(page.getAccount()).getId());
        return ResponseEntity.ok(userService.recordParkDailyInfo(ps));
    }
    //查询园区日常信息记录及园区治疗物使用记录
    @RequestMapping("/getParkStateHistory")
    public ResponseEntity<?> getParkStateHistory(@RequestBody ParkState ps) {
        Page page =ps.getPageList().get(0);
        ps.setUserId(userService.getUser(page.getAccount()).getId());
        return ResponseEntity.ok(userService.getParkStateHistory(ps));
    }
//园区分配
//    园区分配
@RequestMapping("/setParkToUser")
public ResponseEntity<?> setParkToUser(@RequestBody List<ParkDetails> ps,@RequestParam int ParkStateId) {
        if(ParkStateId==1){
            return  ResponseEntity.ok(userService.setParkToUserOk(ps));
        }else if(ParkStateId==3) {
            return ResponseEntity.ok(userService.setParkToUserNO(ps));
        }else {
            return  ResponseEntity.ok(-1);
        }
}
    //人员表数据操作
        //添加员工数据
    @RequestMapping("/insertUserDate")
    public ResponseEntity<?> insertUserDate(@RequestBody User u) {
        return ResponseEntity.ok( userService.setUser(u));
    }

    //员工个人工作计划插入
    @RequestMapping("/insertUserPlan")
    public ResponseEntity<?> insertUserPlan(@RequestBody Plan p) {
        Page page =p.getPageList().get(0);
        p.setUserId(userService.getUser(page.getAccount()).getId());
        return ResponseEntity.ok(userService.setPlan(p));
    }
//    删除计划
    @RequestMapping("/deleteUserPlan")
    public ResponseEntity<?> deleteUserPlan(@RequestBody Plan p) {
        return ResponseEntity.ok(userService.deleteUserPlan(p));
    }

    //员工个人近期工作计划查看
    @RequestMapping("/getUserPlan")
    public ResponseEntity<?> getUserPlan(@RequestBody Plan p) {
        Page page =p.getPageList().get(0);
        p.setUserId(userService.getUser(page.getAccount()).getId());
        return ResponseEntity.ok(userService.getPlantList(p));
    }
    //员工个人全部工作计划查看
    @RequestMapping("/getUserAllPlan")
    public ResponseEntity<?> getUserAllPlan(@RequestBody Plan p) {
        Page page =p.getPageList().get(0);
        p.setUserId(userService.getUser(page.getAccount()).getId());
        return ResponseEntity.ok(userService.getAllPlantList(p));
    }


    //查看园区信息
//    @RequestMapping("/getPark")
//    public ResponseEntity<?> getParkPlant(@RequestBody Plan vo) {
//        return ResponseEntity.ok(userService.setPlan(vo));
//    }


    //查肥料
    @RequestMapping("/getFertilizers")
    public ResponseEntity<?> getFertilizers() {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("Fertilizer",userService.getFertilizer());
        return ResponseEntity.ok(response);
    }

    //查农药
    @RequestMapping("/getPesticides")
    public ResponseEntity<?> getPesticides() {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("Pesticide",userService.getPesticide());
        return ResponseEntity.ok(response);
    }
    //查种子
    @RequestMapping("/getSeedsss")
    public ResponseEntity<?> getSeedsss(@RequestBody seeds vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        response.put("seedList",userService.getSeed(vo));
        return ResponseEntity.ok(response);
    }



//插入园区计划
    @RequestMapping("/setParkPlant")
    public ResponseEntity<?> setParkPlant(@RequestBody CropsVO vo) {
        return ResponseEntity.ok(userService.ParkPlant(vo));
    }

}
